###############################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
###############################################################
# Figure 6: Defence Procurement: Individual-level heterogeneity
###############################################################

# define which packages are needed 
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

# load data
load("EUP_Costs_ReplicationData.RData")


# Interaction Model: Experiment 2
knowledge_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job Cuts` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * quiz + costExperiment2_Treat2 * quiz + QCOUNTRY, data = .)

# Defence Procurement: Job Cuts x Political Knowledge
plot_knowledge_Ex2_1 <- plot_model(knowledge_ex2, type = "pred", terms = c("quiz", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Political Knowledge about the EU", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))


# Defence Procurement: Budgetary Costs x Political Knowledge
plot_knowledge_Ex2_2 <- plot_model(knowledge_ex2, type = "pred", terms = c("quiz", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Political Knowledge about the EU", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 



# Interaction Model: Experiment 2
EUatt_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job losses` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * attitudes_EU + costExperiment2_Treat2 * attitudes_EU + QCOUNTRY, data = .)



# Defence Procurement: Job Cuts x Attitudes towards the EU
plot_EUatt_Ex2_1 <- plot_model(EUatt_ex2, type = "pred", terms = c("attitudes_EU", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Attitudes towards the EU", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 


# Defence Procurement: Budgetary Costs x Attitudes towards the EU
plot_EUatt_Ex2_2 <- plot_model(EUatt_ex2, type = "pred", terms = c("attitudes_EU", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Attitudes towards the EU", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 


# Experiment 2: Interaction Model
MI_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job losses` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * militarism + costExperiment2_Treat2 * militarism + QCOUNTRY, data = .)


# Defence Procurement: Job Cuts x Militant Internationalism
plot_MI_Ex2_1 <- plot_model(MI_ex2, type = "pred", terms = c("militarism", "costExperiment2_Treat1"), colors = c("#87a96b", "#800080")) +
  labs(y = "Predicted Value", x = "Militant Internationalism", title = "Support for Defence Procurement", color = "Non-Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))


# Defence Procurement: Budgetary Costs x Militant Internationalism
plot_MI_Ex2_2 <- plot_model(MI_ex2, type = "pred", terms = c("militarism", "costExperiment2_Treat2"), colors = c("#87a96b", "#ffb653", "#6b87a9")) +
  labs(y = "Predicted Value", x = "Militant Internationalism", title = "Support for Defence Procurement", color = "Budgetary Costs") +
  lims(y = c(0.15,0.8)) +
  theme_ipsum_rc(grid="XY",
                 axis_title_size = 16,
                 axis_text_size = 14,
                 plot_title_size = 20) +
  theme(text = element_text(size = 13),
        legend.key.size = unit(1.5,"line"),
        legend.text = element_text(size = 16),
        legend.title = element_text(size = 16),
        plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm")) 


# add whitespace to arrange plots
whitespace <- grob("")

# define function to arrange plots
grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
  
  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)
  
  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  
  grid.newpage()
  grid.draw(combined)
  
  # return gtable invisibly
  invisible(combined)
  
}

# Plots: Defence Procurement, Non-Budgetary Costs
plot_aircraft_procurement_1 <- grid_arrange_shared_legend(plot_knowledge_Ex2_1,
                                                          plot_EUatt_Ex2_1,
                                                          plot_MI_Ex2_1,
                                                          ncol = 3)
# Plots: Defence Procurement, Budgetary Costs
plot_aircraft_procurement_2 <- grid_arrange_shared_legend(plot_knowledge_Ex2_2,
                                                          plot_EUatt_Ex2_2,
                                                          plot_MI_Ex2_2,
                                                          ncol = 3)

# Conbine Non-Budgetary and Budgetary Costs Plot into one Figure
aircraft_procurement <- grid.arrange(plot_aircraft_procurement_1,
                                     whitespace,
                                     plot_aircraft_procurement_2,
                                     heights=c(20,1.5,20))

# save Figure
ggsave("plots/het_treatment_defence.png", aircraft_procurement,
       device = "jpeg", width = 4600 , height = 3000, units = "px")
